﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.EzAppApi.Utility.EzDeskAppApi;
using EzDesk.EzAppApi.Utility.Models;

namespace EzDesk.EzAppApi.Utility
{
    // 客户端App使用AppService的帮助类
    // 考虑可以在这个地方实现本地Cache，减少WebService请求次数
    public class EzApiClient : EzDesk.EzAppApi.Utility.IEzApiClient
    {
        IDictionary<Guid, SimpleUser> simpleUserCache;
        IDictionary<Guid, Dept> deptCache;

        CoreAppServiceClient _coreAppService;

        public EzApiClient()
        {
            _coreAppService = new CoreAppServiceClient();

            simpleUserCache = new Dictionary<Guid, SimpleUser>();
        }


        public UserProfile GetUserProfileById(Guid userId)
        {
            return _coreAppService.GetUserProfileById(userId);
        }

        public IList<SimpleUser> GetSimpleUserInfos(string memberstring)
        {
            return _coreAppService.GetSimpleUserInfos(memberstring);
        }

        public IList<Dept> GetAllDepts()
        {
            // 如果超时 否则不刷新
            if (!isCacheAvailable())
            {
                refreshDeptCache();
            }
            return deptCache.Values.ToList();
        }

        public Dept GetDeptById(Guid id)
        {
            if (!isCacheAvailable())
            {
                refreshDeptCache();
            }
            return deptCache[id];            
        }

        private bool isCacheAvailable()
        {
            return deptCache != null && deptCache.Count > 0;
        }

        private void refreshDeptCache()
        {
            deptCache = new Dictionary<Guid, Dept>();

            var allDept = _coreAppService.GetAllDepts();
            deptCache.Clear();
            foreach (var dept in allDept)
            {
                deptCache.Add(dept.Id, dept);
            }
        }

        public IList<SimpleUser> GetAllUsersByMemberString(string memberString)
        {
            return _coreAppService.GetAllUsersByMemberString(memberString);
        }
        
        public IDictionary<Guid, string> GetChildrenByMemberString(string memberString)
        {
            return _coreAppService.GetChildrenByMemberString(memberString);
        }

        public IList<Group> GetAllSystemGroups()
        {
            return _coreAppService.GetAllSystemGroups();
        }

        public Guid SendFeed(string feedTypeStr, Guid templateId, Guid? senderId, string feedData, string memberString, bool sendToSelf)
        {
            return _coreAppService.SendFeed(feedTypeStr, templateId, senderId, feedData, memberString, sendToSelf);
        }
        
        public void UpdateFeedData(Guid updateFeedId, string newFeedData)
        {
            _coreAppService.UpdateFeedData(updateFeedId, newFeedData);
        }

        public void SetFeedIsAvailable(Guid feedId, string memberString, bool isAvailable)
        {
            _coreAppService.SetFeedIsAvailable(feedId, memberString, isAvailable);
        }

        public SimpleUser GetSimpleUserById(Guid userId)
        {
            if (simpleUserCache.ContainsKey(userId))
            {
                return simpleUserCache[userId];
            }
            else
            {
                SimpleUser user = _coreAppService.GetSimpleUserById(userId);
                simpleUserCache.Add(userId, user);
                return user;
            }
        }

        public Group GetGroupById(Guid groupId)
        {
            return _coreAppService.GetGroupById(groupId);
        }

        public IList<Group> GetGroups(string groupIdStrs)
        {
            return _coreAppService.GetGroups(groupIdStrs);
        }

        public void SetFeedsIsAvailable(string feedIdStrs, string memberstring, bool isAvailable)
        {
            _coreAppService.SetFeedsIsAvailable(feedIdStrs, memberstring, isAvailable);
        }

        public Group GetGroupByMemberstring(string memberstring)
        {
            return _coreAppService.GetGroupMemberstring(memberstring);
        }

        public void UpdateFeedsData(string feedIdsStr, string updateDataStr)
        {
            _coreAppService.UpdateFeedsData(feedIdsStr, updateDataStr);
        }

        public void UpdatePersonalFeedData(Guid feedId, string memberstring, string feedDataEx)
        {
            _coreAppService.UpdatePersonalFeedData(feedId, memberstring, feedDataEx);
        }
    }
}
